package com.pig4cloud.pig.ai.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.ai.api.vo.CollectionVO;
import com.pig4cloud.pig.ai.api.vo.DataVO;
import com.pig4cloud.pig.ai.api.vo.KnowledgeVO;
import com.pig4cloud.pig.ai.service.knowZskService;
import com.pig4cloud.pig.ai.utils.AiUtils;

import lombok.AllArgsConstructor;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.ss.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * packageName com.pig4cloud.pig.base.service.impl
 *
 * @author marco
 * @className BaseServiceImpl (此处以class为例)
 * @date 2024/5/30
 * @description TODO
 */
@Service
@AllArgsConstructor
public class knowZskServiceImpl implements knowZskService {

	@Autowired
	private AiUtils aiUtils;

	@Override
	public Page fastList(Page page,KnowledgeVO knowledgeVO) {
		String jsonpage = aiUtils.list();
		if(StringUtil.isNotBlank(jsonpage)) {
			JSONArray jsonObject = JSON.parseArray(jsonpage);
			System.out.println(jsonObject);
			List<KnowledgeVO> list = JSON.parseArray(jsonpage, KnowledgeVO.class);
			int total = list.size();
			page.setTotal(Long.valueOf(total));
			page.setRecords(list);
		}
		return page;
	}

	@Override
	public Boolean saveCreate(KnowledgeVO knowledgeVO) {
		boolean booleanConfig;
		String config = aiUtils.create(knowledgeVO.getName(),knowledgeVO.getIntro());
		if (StringUtil.isNotBlank(config)) {
			booleanConfig = true;
		} else {
			// 处理无效输入或抛出异常
			booleanConfig = false;
		}
		return booleanConfig;
	}
	
	@Override
	public KnowledgeVO getByid(String id) {
		String config = aiUtils.detail(id);
		JSONObject jsonObject = JSONObject.parseObject(config);
		KnowledgeVO configVO=JSON.toJavaObject(jsonObject, KnowledgeVO.class);
		return configVO;
	}
	
	@Override
	public Boolean deleteByid(String id) {
		boolean booleanConfig;
		String config = aiUtils.delete(id);
		if (config.equals("200")) {
			booleanConfig = true;
		} else {
			// 处理无效输入或抛出异常
			booleanConfig = false;
		}
		return booleanConfig;
	}
	
	
	@Override
	public Page dataSetList(Page page,CollectionVO collectionVO) {
		String jsonpage = aiUtils.dataSetList(collectionVO.getTmbId(),(int)page.getCurrent(),(int)page.getSize(),collectionVO.getName());
		if(StringUtil.isNotBlank(jsonpage)) {
			JSONObject jsonObject = JSONObject.parseObject(jsonpage);
			String pageItems = String.valueOf(jsonObject.get("data"));
			System.out.println(pageItems);
			List<CollectionVO> list = JSON.parseArray(pageItems, CollectionVO.class);
			String total = jsonObject.getString("total");
			page.setTotal(Long.parseLong(total));
			page.setRecords(list);
		}
		return page;
	}

	
	
	@Override
	public Boolean collectionCreate(CollectionVO collectionVO) {
		boolean booleanConfig;
		String config = aiUtils.collectionCreate(collectionVO.getTmbId(),collectionVO.getName());
		if (StringUtil.isNotBlank(config)) {
			booleanConfig = true;
		} else {
			// 处理无效输入或抛出异常
			booleanConfig = false;
		}
		return booleanConfig;
	}
	

	@Override
	public Boolean filepushData(MultipartFile file, String tmbId) {
		//首先
		List<Map<String, Object>> listobj = new ArrayList<Map<String,Object>>();
		try (InputStream inputStream = file.getInputStream();
	             Workbook workbook = new XSSFWorkbook(inputStream)) {
	            Sheet sheet = workbook.getSheetAt(0);
	            
	            for (Row row : sheet) {
	            	Map<String, Object> obj = new HashMap<String, Object>();
	            	String key="";
		            String context = "";
		            int k = 0;
	                for (Cell cell : row) {
	                	k++;
	                	if(k == 1) {
	                		key = cell.getStringCellValue();
	                	}else {
	                		context = cell.getStringCellValue();
	                	}
	                }
	                obj.put(key, context);
	                listobj.add(obj);
	            }
	            
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
		
		boolean booleanConfig;
		//根据知识库id获取手动数据集返回值，
		String originalFilename = file.getOriginalFilename();
        int dotIndex = originalFilename.indexOf(".");
        String result ="";
        if (dotIndex != -1) {
             result = originalFilename.substring(0, dotIndex);
        }
        
		String collectionCreate = aiUtils.collectionCreate(tmbId, result);
		List<Object> obj = aiUtils.pushData(collectionCreate,listobj);
		if(obj.get(0).equals("200")) {
			booleanConfig = true;
		}else {
			booleanConfig = false;
		}
		return booleanConfig;
	}
	
	
	@Override
	public Boolean createText(CollectionVO collectionVO) {
		boolean booleanConfig;
		String config = aiUtils.createText(collectionVO.getTmbId(),collectionVO.getName(),collectionVO.getFileName());
		if (StringUtil.isNotBlank(config)) {
			booleanConfig = true;
		} else {
			// 处理无效输入或抛出异常
			booleanConfig = false;
		}
		return booleanConfig;
	}
	
	
	@Override
	public Page datasetcoreList(Page page,DataVO dataVO) {
		String jsonpage = aiUtils.dataList(dataVO.getDatasetId(),(int)page.getCurrent(),(int)page.getSize(),"");
		if(StringUtil.isNotBlank(jsonpage)) {
			JSONObject jsonObject = JSONObject.parseObject(jsonpage);
			String pageItems = String.valueOf(jsonObject.get("data"));
			String total = jsonObject.getString("total");
			
			System.out.println(pageItems);
			List<DataVO> list = JSON.parseArray(pageItems, DataVO.class);
			page.setTotal(Long.parseLong(total));
			page.setRecords(list);
		}
		return page;
	}
	
}
